package matlap.client.view.admin;
import java.util.Iterator;
import java.util.List;
import matlap.client.services.school.SchoolService;
import matlap.client.services.school.SchoolServiceAsync;
import matlap.shared.School;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class SchoolsComposite extends Composite {

	SchoolServiceAsync c1 = GWT.create(SchoolService.class);
	private VerticalPanel vp;
	private Panel gridPanel;
	private Panel newPanel;
	private Grid g;
	private Label lbName;
	private TextBox tbName;
	private ListBox cbLocality;
	private Button btnSearch;
	private Label lbLocality;
	protected String name;
	protected String locality;
	private Label nameLabel;
	private TextBox textBox;
	private ListBox comboBox;
	private Button btnNewButton;
	private Label lblNewLabel;
	private Label lblNewLabel_1;
	private VerticalPanel verticalPanel;
	private VerticalPanel verticalPanel_1;

	public SchoolsComposite() {
		initNewPanel();
		init();
		initGrid();

	}

	public void initNewPanel() {
		newPanel = new VerticalPanel();
		DecoratorPanel decoratorPanel = new DecoratorPanel();
		newPanel.add(decoratorPanel);
		Label lblNewLabel_2 = new Label("\u00DAj iskola:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);
		
		verticalPanel = new VerticalPanel();
		verticalPanel.setBorderWidth(1);
		newPanel.add(verticalPanel);
		verticalPanel.setWidth("668px");

		FlexTable fl = new FlexTable();
		verticalPanel.add(fl);
		fl.setSize("663px", "38px");

		Label vnLabel = new Label("Iskola neve");
		fl.setWidget(0, 0, vnLabel);
		vnLabel.setSize("190px", "23px");

		nameLabel = new Label("Helys\u00E9g");
		fl.setWidget(0, 1, nameLabel);

		textBox = new TextBox();
		fl.setWidget(1, 0, textBox);

		comboBox = new ListBox();
		comboBox.addItem("");
		fl.setWidget(1, 1, comboBox);
		
				btnNewButton = new Button("New button");
				btnNewButton.setText("Ment\u00E9s");
				btnNewButton.addClickHandler(new ClickHandler() {

					public void onClick(ClickEvent event) {
						final String name = textBox.getText();
						String loc = comboBox.getItemText(comboBox.getSelectedIndex());

						if ((name.length() == 0) || (loc.length() == 0)) {
							Window.alert("Nem adott meg minden adatot!");
						} else {
							nameLabel.setText("");
							comboBox.setSelectedIndex(0);
							c1.insertSchool(name, loc, new AsyncCallback<Void>() {

								public void onFailure(Throwable caught) {
									Window.alert("Hiba");

								}

								public void onSuccess(Void result) {

									textBox.setText("");
									comboBox.setSelectedIndex(0);
									Window.alert("Sikeres");
									initLocalityBox();
									gridPanel.clear();
									initGrid();

								}

							});
						}
					}

				});
				fl.setWidget(1, 2, btnNewButton);
				fl.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_RIGHT);
		c1.selectLocality(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Error in Teacher");
			}

			public void onSuccess(List<String> result) {
				Iterator<String> it = result.iterator();
				while (it.hasNext()) {
					String str = (String) it.next();
					comboBox.addItem(str);
				}

			}

		});

	}

	private void init() {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("670px", "500px");

		vp.add(newPanel);
		newPanel.setSize("668px", "");

		DecoratorPanel decoratorPanel = new DecoratorPanel();
		vp.add(decoratorPanel);
		Label lblNewLabel_2 = new Label("Jelenlegi iskol\u00E1k:");
		lblNewLabel_2.setStyleName("BigBold");
		decoratorPanel.setWidget(lblNewLabel_2);
		
		verticalPanel_1 = new VerticalPanel();
		verticalPanel_1.setBorderWidth(1);
		vp.add(verticalPanel_1);
		verticalPanel_1.setWidth("668px");
		FlexTable filterFlex = new FlexTable();
		verticalPanel_1.add(filterFlex);
		filterFlex.setSize("664px", "39px");

		lbName = new Label("N\u00E9v");
		filterFlex.setWidget(0, 0, lbName);
		lbName.setWidth("53px");

		lbLocality = new Label("Helys\u00E9g");
		filterFlex.setWidget(0, 1, lbLocality);

		tbName = new TextBox();
		filterFlex.setWidget(1, 0, tbName);
		tbName.setWidth("333px");

		cbLocality = new ListBox();
		filterFlex.setWidget(1, 1, cbLocality);
		
				btnSearch = new Button("New button");
				btnSearch.setText("Keres");
				filterFlex.setWidget(1, 2, btnSearch);
				filterFlex.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_RIGHT);
				
						btnSearch.addClickHandler(new ClickHandler() {
				
							public void onClick(ClickEvent event) {
								name = tbName.getText();
				
								int i = cbLocality.getSelectedIndex();
								locality = cbLocality.getItemText(i);
				
								c1.getSchoolsBy(name, locality,
										new AsyncCallback<List<School>>() {
				
									@Override
									public void onFailure(Throwable caught) {
				
									}
				
									@Override
									public void onSuccess(List<School> result) {
										int i = 1;
										Iterator<School> it = result.iterator();
										g.resize(result.size() + 1, 2);
										while (it.hasNext()) {
											School teach = (School) it.next();
				
											g.setText(i, 0, teach.getName());
											g.setText(i, 1, teach.getLocality());
											i++;
				
										}
				
									}
								});
							}
						});
						


							btnSearch.addClickHandler(new ClickHandler() {

								public void onClick(ClickEvent event) {
									name = tbName.getText();

									int i = cbLocality.getSelectedIndex();
									locality = cbLocality.getItemText(i);

									c1.getSchoolsBy(name, locality,
											new AsyncCallback<List<School>>() {

										@Override
										public void onFailure(Throwable caught) {

										}

										@Override
										public void onSuccess(List<School> result) {
											int i = 1;
											Iterator<School> it = result.iterator();
											g.resize(result.size() + 1, 2);
											while (it.hasNext()) {
												School teach = (School) it.next();

												g.setText(i, 0, teach.getName());
												g.setText(i, 1, teach.getLocality());
												i++;

											}

										}
									});
								}
							});
		initLocalityBox();
	}

	public void initGrid() {

		g = new Grid(1, 2);
		Label lb0 = new Label("N\u00E9v");
		lb0.setStyleName("Header");
		g.setWidget(0, 0, lb0);
		
		Label lb1 = new Label("Helys\u00E9g");
		lb1.setStyleName("Header");
		g.setWidget(0, 1, lb1);
		
		g.setBorderWidth(2);
		gridPanel = new VerticalPanel();
		gridPanel.add(g);
		vp.add(gridPanel);
		gridPanel.setSize("668px", "34px");
		g.setSize("668px", "34px");

		lblNewLabel = new Label("N\u00E9v");
		lblNewLabel.setStyleName("Header");
		g.setWidget(0, 0, lblNewLabel);

		lblNewLabel_1 = new Label("Helys\u00E9g");
		lblNewLabel_1.setStyleName("Header");
		g.setWidget(0, 1, lblNewLabel_1);

		c1.getSchoolsNumber(new AsyncCallback<Integer>() {

			public void onFailure(Throwable caught) {
				Window.alert("Gond van volume");

			}

			public void onSuccess(Integer result) {
				g.resize(result + 1, 2);
			}
		});

		c1.getSchools(new AsyncCallback<List<School>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Gond van School");

			}

			public void onSuccess(List<School> result) {
				int i = 1;
				Iterator<School> it = result.iterator();
				while (it.hasNext()) {
					School distr = (School) it.next();

					g.setText(i, 0, distr.getName());
					g.setText(i, 1, distr.getLocality());
					i++;

				}

			}

		});

	}

	public void initLocalityBox(){
		cbLocality.clear();
		cbLocality.addItem("");
		c1.selectSchoolsLocality(new AsyncCallback<List<School>>() {

			public void onFailure(Throwable caught) {
				Window.alert("Error in Teacher");
			}

			public void onSuccess(List<School> result) {
				Iterator<School> it = result.iterator();
				while (it.hasNext()) {
					School stu = (School) it.next();
					cbLocality.addItem(stu.getLocality());
				}

			}

		});
	}
}
